package com.magicsoft.share.ui.my;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.ArticleListAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.presenter.my.AllSharePresenter;
import com.magicsoft.share.ui.article.AddArticleActivity;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.my.IAllShareView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.OnClick;

public class AllShareActivity extends BaseActivity<IAllShareView, AllSharePresenter> implements IAllShareView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;

    public static final byte UPLOADED = 1;//已上传
    public static final byte DRAFT = 2;//草稿箱

    private byte shareType = UPLOADED;
    private ArticleListAdapter articleListAdapter;
    private int pageNum = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;
    private int position;

    @Override
    public AllSharePresenter initPresenter() {
        return new AllSharePresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_all_share;
    }

    @Override
    public void initData() {

        position = getIntent().getIntExtra("position", 0);
    }

    @Override
    public void initView() {
        tvTitle.setText("全部分享");
        setIndicator(this, tabLayout, 60, 60);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String s = tab.getText().toString();
                if (s.equals("已上传")) {
                    shareType = UPLOADED;
                    requestData();
                } else {
                    shareType = DRAFT;
                    requestData();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        initRV();

        tabLayout.getTabAt(position).select();


    }

    @Override
    public void requestData() {

        mPresenter.myArticleList(pageNum, pageSize, shareType);


    }


    @OnClick(R.id.ibtn_left)
    public void onViewClicked() {
        finish();
    }

    private void initRV() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg2), 0, DensityUtils.dp2px(this, 10)));
        articleListAdapter = new ArticleListAdapter(R.layout.item_article);
        articleListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                long articleId = articleListAdapter.getItem(position).getArticleId();
                if (shareType == UPLOADED) {
                    Intent intent = new Intent(AllShareActivity.this, ArticleShareActivity.class);

                    intent.putExtra("articleId", articleId);
                    Log.e("Data",articleId+"");
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(AllShareActivity.this, AddArticleActivity.class);
                    intent.putExtra("articleId",articleId);
                    Log.e("Data1",articleId+"");
                    startActivity(intent);

                }
            }
        });

        recyclerView.useDefaultLoadMore();
        recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                isRefresh = false;

                pageNum++;
                requestData();
            }
        });
        recyclerView.setAdapter(articleListAdapter);
    }

    @Override
    public void myArticleListSuccess(ArticleList data) {
        if (isRefresh) {
            articleListAdapter.setNewData(data.getList());
        } else {
            articleListAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }


    }

    @Override
    public void myArticleListFail() {

        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }


    public void setIndicator(Context context, TabLayout tabs, float leftDip, float rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;//反射
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (getDisplayMetrics(context).density * leftDip);
        int right = (int) (getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    public DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric;
    }

}
